{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Excel Sheet Column Title"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: convertToTitle"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #Excel表列名称"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数 <code>columnNumber</code> ，返回它在 Excel 表中相对应的列名称。</p>\n",
    "\n",
    "<p>例如：</p>\n",
    "\n",
    "<pre>\n",
    "A -> 1\n",
    "B -> 2\n",
    "C -> 3\n",
    "...\n",
    "Z -> 26\n",
    "AA -> 27\n",
    "AB -> 28 \n",
    "...\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>columnNumber = 1\n",
    "<strong>输出：</strong>\"A\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>columnNumber = 28\n",
    "<strong>输出：</strong>\"AB\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>columnNumber = 701\n",
    "<strong>输出：</strong>\"ZY\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>columnNumber = 2147483647\n",
    "<strong>输出：</strong>\"FXSHRXW\"\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= columnNumber <= 2<sup>31</sup> - 1</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [excel-sheet-column-title](https://leetcode.cn/problems/excel-sheet-column-title/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [excel-sheet-column-title](https://leetcode.cn/problems/excel-sheet-column-title/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['1', '28', '701']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, n):\n",
    "        \"\"\"\n",
    "        :type n: int\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        \n",
    "        from collections import deque\n",
    "        column = deque()\n",
    "        while n > 0:\n",
    "            n, result = divmod(n-1, 26)\n",
    "            column.appendleft(result)\n",
    "            \n",
    "        return ''.join([chr(i+ord('A')) for i in column])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, n):\n",
    "        \"\"\"\n",
    "        :type n: int\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        s = 'ZABCDEFGHIJKLMNOPQRSTUVWXY'\n",
    "        l = []\n",
    "        while n:\n",
    "            i = n % 26\n",
    "            n = n // 26\n",
    "            if i == 0:\n",
    "                n = n - 1\n",
    "            l.append(s[i])\n",
    "        return ''.join(reversed(l))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, n: 'int') -> 'str':\n",
    "        ret = ''\n",
    "        \n",
    "        while n:\n",
    "            k = (n - 1) % 26 + 1\n",
    "            n = (n - k) // 26\n",
    "            ret = chr(64 + k) + ret\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def func(x):\n",
    "    y = 26\n",
    "    l = []\n",
    "    while x:\n",
    "        rem = x % y\n",
    "        if rem:\n",
    "            l.append(rem)\n",
    "            x //= y\n",
    "        else:\n",
    "            l.append(26)\n",
    "            x -= 26\n",
    "            x //= y\n",
    "    l.reverse()\n",
    "    return l\n",
    "\n",
    "char = ['A','B','C','D','E','F','G','H','I','J','K','L',\n",
    "'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, n):\n",
    "        \"\"\"\n",
    "        :type n: int\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        l = func(n)\n",
    "        s = \"\"\n",
    "        for i in l:\n",
    "            s += char[i-1]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, n):\n",
    "        \"\"\"\n",
    "        :type n: int\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        radix = 26\n",
    "        int2char = [''] * radix\n",
    "\n",
    "        for i in range(0,radix):\n",
    "            int2char[i] = chr(ord('A') + i)\n",
    "        title = []\n",
    "        n-=1\n",
    "        while True:\n",
    "            title.append(int2char[n % radix])\n",
    "            n //= radix\n",
    "            n-=1\n",
    "            if n<0:\n",
    "                break\n",
    "        return ''.join(reversed(title))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, n):\n",
    "        \"\"\"\n",
    "        :type n: int\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        chars_map = {i-ord('A'): chr(i) for i in range(ord('A'), ord('Z') + 1)}\n",
    "        # chars_map[0] = 'Z'\n",
    "        base = len(chars_map)\n",
    "        \n",
    "        s = ''\n",
    "        while(n>0):\n",
    "            e = n % base\n",
    "            if e==0:\n",
    "                s += 'Z'\n",
    "                n = n//base-1\n",
    "            else:\n",
    "                s += chars_map[e-1]\n",
    "                n = n // base\n",
    "        return s[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, n: int) -> str:\n",
    "        # A的ascii码为65\n",
    "        s = ''\n",
    "        while(n):\n",
    "            n -= 1\n",
    "            s = chr(n%26+65) + s\n",
    "            n = n//26\n",
    "        return s\n",
    "        \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, n: int) -> str:\n",
    "        res = ''\n",
    "        while n:\n",
    "            n -= 1\n",
    "            s,y = divmod(n,26)\n",
    "            res = chr(y+65) + res\n",
    "            n = s\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, n: int) -> str:\n",
    "        return \"\" if n == 0 else self.convertToTitle((n-1)//26) + chr((n-1)%26 + 65)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, n: int) -> str:\n",
    "        t = n\n",
    "        s = ''\n",
    "        while t > 0:                # 每次循环 是 每次取模得到数字 的过程\n",
    "            t -= 1                  # 把 从1开始满27进位 变回 从0开始满26进位\n",
    "            a, b = t//26, t%26      # 这里b的取值范围是0-25\n",
    "            s = s + chr(b+65)       # A的ASCII码是65，b+65表示 0-25 和 A-Z 有了一一对应\n",
    "            t = a\n",
    "        return s[::-1]              # 最后将 优先获得的低位数字 反转到最后"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, n: int) -> str:\n",
    "        result = ''\n",
    "        while n > 0:\n",
    "            if n % 26 != 0:\n",
    "                result += (chr(ord('A') + int(n % 26) - 1))\n",
    "                n = int(n / 26)\n",
    "            else:\n",
    "                result += (chr(ord('A') + 25))\n",
    "                n = int(n / 26) - 1\n",
    "        return result[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, n: int) -> str:\n",
    "        # res = ''\n",
    "        # while n > 0:                # 每次循环 是 每次取模得到数字 的过程\n",
    "        #     n -= 1                  # 把 从1开始满27进位 变回 从0开始满26进位\n",
    "        #     a, b = n // 26, n % 26      # 这里b的取值范围是0-25\n",
    "        #     res = res + chr(b + 65)       # A的ASCII码是65，b+65表示 0-25 和 A-Z 有了一一对应\n",
    "        #     n = a\n",
    "        # return res[::-1]              # 最后将 优先获得的低位数字 反转到最后\n",
    "        res = []\n",
    "        while n > 0:\n",
    "            n -= 1\n",
    "            res.append(chr(n % 26 + ord('A')))\n",
    "            n = n // 26\n",
    "        return ''.join(res[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, n: int) -> str:\n",
    "        s=\"\"\n",
    "        list=['A','B','C','D','E','F','G','H','I','J','K','L','M','N',\n",
    "                'O','P','Q','R','S','T','U','V','W','X','Y','Z']\n",
    "        while n>26:\n",
    "            x=n%26\n",
    "            s+=list[x-1]\n",
    "            if x==0:\n",
    "                n=n//26-1\n",
    "            else:\n",
    "                n=n//26\n",
    "            if n<26:\n",
    "                break\n",
    "        res=\"\"\n",
    "        res+=list[n-1]\n",
    "        for i in range(len(s)-1,-1,-1):\n",
    "            res+=s[i]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, n: int) -> str:\n",
    "        s = ''\n",
    "        while n:\n",
    "            n -= 1\n",
    "            s = chr(n % 26 + 65) + s\n",
    "            n = n // 26\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, columnNumber: int) -> str:\n",
    "        res = ''\n",
    "        while columnNumber > 0:\n",
    "            cur = columnNumber % 26\n",
    "            if cur == 0:\n",
    "                res += 'Z'\n",
    "                columnNumber //= 26\n",
    "                columnNumber -= 1\n",
    "                continue\n",
    "            res += chr(64+cur)\n",
    "            columnNumber //= 26\n",
    "        return res[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, columnNumber: int) -> str:\n",
    "        ans = list()\n",
    "        while columnNumber>0:\n",
    "            a0 = (columnNumber-1)%26+1\n",
    "            ans.append(chr(a0-1+ord(\"A\")))\n",
    "            columnNumber=(columnNumber-a0)//26\n",
    "        return \"\".join(ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, columnNumber: int) -> str:\n",
    "        t = columnNumber\n",
    "        l = 0\n",
    "        while True:\n",
    "            l += 1\n",
    "            if t <= 26**l:\n",
    "                break\n",
    "            else:\n",
    "                t -= 26**l\n",
    "        # 输出字符长度为l,t为当前剩余字符\n",
    "        ans = ''\n",
    "        while l>0:\n",
    "            l = l-1\n",
    "            pos = (t-1)//(26**l)\n",
    "            if l !=0:\n",
    "                ans += chr(ord('A')+int(pos))\n",
    "            else:\n",
    "                ans += chr(ord('A')+int(pos))\n",
    "            t = (t-1)%(26**l)+1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, columnNumber: int) -> str:\n",
    "        ans = list()\n",
    "        while columnNumber > 0:\n",
    "            a0 = (columnNumber - 1) % 26 + 1\n",
    "            ans.append(chr(a0 - 1 + ord(\"A\")))\n",
    "            columnNumber = (columnNumber - a0) // 26\n",
    "        return \"\".join(ans[::-1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, columnNumber: int) -> str:\n",
    "        def div(columnNumber):\n",
    "            \n",
    "            mod = columnNumber % 26\n",
    "            notmod = columnNumber // 26\n",
    "            if mod == 0:\n",
    "                mod = 26\n",
    "                notmod -= 1\n",
    "            l1.append(mod)\n",
    "            print(notmod)\n",
    "            if notmod == 0:\n",
    "                return l1\n",
    "\n",
    "            div(notmod)\n",
    "        \n",
    "        l1 = []\n",
    "        # columnNumber = 26\n",
    "        div(columnNumber)\n",
    "        print(l1)\n",
    "        # stack = l1.reverse\n",
    "        # print(stack)\n",
    "        x = ord(\"A\") - 1\n",
    "        my_list = []\n",
    "        for i in l1:      \n",
    "            my_list.append(chr(i + x))\n",
    "        print(my_list)\n",
    "        return \"\".join(my_list[::-1])\n",
    "        \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, columnNumber: int) -> str:\n",
    "        res = \"\"\n",
    "        while columnNumber:\n",
    "            columnNumber -= 1\n",
    "            columnNumber , y = divmod(columnNumber,26)\n",
    "            res = chr(y + 65) + res\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, columnNumber: int) -> str:\n",
    "        s = \"\"\n",
    "        while True:\n",
    "            t = (columnNumber - 1) % 26\n",
    "            s = chr(65+t) + s\n",
    "            columnNumber = (columnNumber - 1) // 26\n",
    "            if columnNumber == 0: break\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToTitle(self, columnNumber: int) -> str:\n",
    "        s = \"\"\n",
    "        while columnNumber:\n",
    "            s = chr((columnNumber - 1) % 26 + 65) + s\n",
    "            columnNumber = (columnNumber - 1) // 26\n",
    "        return s\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
